package lab03.text07;
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

/**
 * 读取book.zip,
 * 并将book.zip中含有的文件重新存放到当前目录中的book文件夹中，
 * 即将book.zip的内容解压到book文件夹中
 */
public class Text {
    public static void main(String[] args) throws IOException {
        String path = "F:\\book.zip";

        File outFile = null;
        ZipFile zipFile = new ZipFile(path);

        InputStream input = null;
        OutputStream out = null;
        ZipInputStream zipInputStream = null;

        try {
            zipInputStream = new ZipInputStream(new FileInputStream(path));
            ZipEntry entry;
            while ((entry = zipInputStream.getNextEntry()) != null) {
                System.out.println("解压文件" + entry.getName());

                outFile = new File("F:\\book" + File.separator + entry.getName());
                outFile.createNewFile();

                input = zipFile.getInputStream(entry);
                out = new FileOutputStream(outFile);
                int h = 0;
                while ((h = input.read()) != -1) {
                    out.write(h);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipInputStream.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
